package pt.agap2.relationchat;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ListView;

import pt.agap2.relationchat.utils.MockServerCommunication;
import pt.agap2.relationchat.utils.UserData;
import pt.agap2.relationchat.utils.Constants;

public class UserlistActivity extends Activity implements View.OnClickListener{


    private ListView lv;
    private Button userListActivityRefresh;
    private String appToken;
    private String userToken;
    private MockServerCommunication msc;
    private UserData[] aUserData;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_user_list);

        lv = (ListView) findViewById(R.id.userListActivityLv);
        userListActivityRefresh = (Button) findViewById(R.id.userListActivityRefresh);
        userListActivityRefresh.setOnClickListener(this);
    }

    @Override
    protected void onResume(){
        super.onResume();
        refreshUsers();
    }

    private void refreshUsers(){

        SharedPreferences settings = getSharedPreferences(Constants.PREFS_NAME, 0);
        appToken = settings.getString(Constants.KEY_APP_TOKEN, Constants.APP_TOKEN);
        userToken = settings.getString(Constants.KEY_USER_TOKEN, Constants.EMPTY_STRING);

        msc = new MockServerCommunication();
        aUserData = msc.getUsers(appToken, userToken);

        for (int i = 0; i <aUserData.length; i++){
            UserData ud = aUserData[i];
            Log.d("ARC", String.format("username {0} - {1}", i, ud.userName));
        }
    }

    @Override
    public void onClick(View v){
        switch (v.getId()){
            case R.id.userListActivityRefresh:
                refreshUsers();
                showUserMessageActivity();
                break;
        }
    }

    private void showUserMessageActivity(){
        Intent intent = new Intent(getApplicationContext(), UserMessageActivity.class);
        intent.putExtra("personId", aUserData[0].userId);
        startActivity(intent);
    }
}
